#include "snakes.h"

int main(void)
{
	hideCursor();
	srand(time(NULL));//播种随机数种子
	initSnake();
	initFood();
	initWall();
	initUI();
	playGame(); 

	system("pause");
}

void initSnake(void)
{
	snake.size = 2;

	snake.body[0].x = WIDE / 2;
	snake.body[0].y = HIGH / 2;

	snake.body[1].x = WIDE / 2 - 1;
	snake.body[1].y = HIGH / 2;

	return;
}
void initFood(void)
{
	food.x = rand() % WIDE;
	food.y = rand() % HIGH;

	return;
}
void initUI(void)
{
	COORD coord;

	//画蛇
	for (size_t i = 0; i < snake.size; i++)
	{
		coord.X = snake.body[i].x;
		coord.Y = snake.body[i].y;

		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
		if (i == 0)
		{
			putchar('@');
		}
		else
		{
			putchar('*');
		}
	}
	//处理蛇尾移动
	coord.X = lastBodyX;
	coord.Y = lastBodyY;

	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
	putchar(' ');

	//画食物
	coord.X = food.x;
	coord.Y = food.y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
	putchar('#');

	coord.X = 0;
	coord.Y = HIGH + 3;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void initWall(void)
{
	for (size_t i = 0; i <= HIGH; i++)
	{
		for (size_t j = 0; j <= WIDE; j++)
		{
			if (j == WIDE)
			{
				cout << "|";
			}
			else if (i == HIGH)
			{
				cout << "-";
			}
			else
			{
				cout << " ";
			}
		}
		cout << endl;
	}
}
char control(char key)
{
	if (_kbhit())//接收输入
	{
		key = _getch();
	}
	switch (key)
	{
	case 'w':kx = 0, ky = -1; return 'w'; break;
	case 'a':kx = -1, ky = 0; return 'a'; break;
	case 's':kx = 0, ky = +1; return 's'; break;
	case 'd':kx = +1, ky = 0; return 'd'; break;
	default:
		break;
	}
}
void snakeMove(void)
{
	lastBodyX = snake.body[snake.size - 1].x;
	lastBodyY = snake.body[snake.size - 1].y;//存储蛇尾坐标
	for (size_t i = snake.size - 1; i > 0; i--)//蛇身移动
	{
		snake.body[i].x = snake.body[i - 1].x;
		snake.body[i].y = snake.body[i - 1].y;
	}
	snake.body[0].x += kx;
	snake.body[0].y += ky;//蛇头移动
	//处理蛇尾移动的一部分代码在initUI里面

	Sleep(300);
}
void playGame(void)
{
	char key = 'd';

	while (snake.body[0].x >= 0 && snake.body[0].x < WIDE
		 && snake.body[0].y >= 0 && snake.body[0].y < HIGH)
	{
		initUI();
		key = control(key);
		

		for (size_t i = 1; i < snake.size; i++)//蛇身碰撞
		{
			if (snake.body[0].x == snake.body[i].x && snake.body[0].y == snake.body[i].y)
			{
				printScore();
				return;
			}
		}
		if (snake.body[0].x == food.x && snake.body[0].y == food.y)//食物碰撞
		{
			initFood();
			snake.size++;
			score++;
		
		}
		snakeMove();
	}
	printScore();
	return;
}
void printScore(void)
{
	system("cls");
	cout << "游戏结束！" << endl;
	cout << "得分：" << score << endl;
}

void hideCursor(void)
{
	CONSOLE_CURSOR_INFO consoleCursorInfo;
	consoleCursorInfo.dwSize = sizeof(consoleCursorInfo);
	consoleCursorInfo.bVisible = FALSE;

	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleCursorInfo);
}